using System;
using System.Collections.Generic;
using System.Text;

namespace TST.SharePoint.Shared
{
    public class FieldDateTime : Field
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public FieldDateTime()
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ID">The ID for the field.</param>
        public FieldDateTime(string Id)
            : base(Id)
        {
        }

        /// <summary>
        /// Get the FieldType for the field URL.
        /// </summary>
        /// <returns></returns>
        public override FieldType FieldType
        {
            get
            {
                return FieldType.DateTime;
            }
        }

        /// <summary>
        /// Get the .NET datatype. Always returns DateTime
        /// </summary>
        public override Type DataType
        {
            get
            {
                return typeof(DateTime);
            }
        }

        public override object GetTypedValue(object value)
        {
            if (value == null)
                return null;
            DateTime result;
            if (DateTime.TryParse(value.ToString(), out result))
                return result;
            return null;
        }

        public override FilterType FilterType
        {
            get { return FilterType.DateTime; }
        }

        internal override string GetFilterValue(string value)
        {
            DateTime date;
            if (DateTime.TryParse(value, out date))
                return CreateISO8601DateStringFromSystemDateString(date);
            return string.Empty;
        }

        public static string CreateISO8601DateStringFromSystemDateString(DateTime dateTime)
        {
            return string.Format("{0}-{1}-{2}",
                dateTime.Year.ToString(),
                dateTime.Month.ToString(),
                dateTime.Day.ToString());
        }

    }
}
